// 1、引入要用到的api
import { defineStore } from 'pinia';

// 引入接口
import { reqGetCategory1ListData, reqGetCategory2ListData, reqGetCategory3ListData } from '@/api/product/category';

// 引入TS约束
import type { CategoryStateModel } from '@/api/product/model/categoryModel';

// 2、创建模块对象
export const useCategoryListStore = defineStore('categoryList', {
  state: (): CategoryStateModel => {
    return {
      // 以下三个属性用于手机用户真正选择的分类的id
      category1Id: undefined,
      category2Id: undefined,
      category3Id: undefined,

      // 以下三个数组用于存储服务器响应回来的分类数据
      category1List: [],
      category2List: [],
      category3List: [],
    }
  },
  actions: {
    // 1、获取一级分类数据
    async getCategory1ListData() {
      const res = await reqGetCategory1ListData();
      // console.log(res);
      this.category1List = res;
    },
    // 2、获取二级分类数据
    async getCategory2ListData(id: number) {
      this.category1Id = id;
      // console.log(111);

      // 如果一级导航更改，则二级和三级导航也需要随之变化
      this.category2Id = undefined;
      this.category3Id = undefined;
      this.category3List = [];

      this.category2List = await reqGetCategory2ListData(id);
    },
    // 3、获取三级分类数据
    async getCategory3ListData(id: number) {
      this.category2Id = id;

      // 如果二级导航改变，则三级导航也需要改变
      this.category3Id = undefined;

      this.category3List = await reqGetCategory3ListData(id);
    },
    // 4、设置三级导航的ID
    setCategory3Id(val: number) {
      this.category3Id = val;
    }
  }
})


